class_name GravityAttackTip
extends Node2D


@export var countdown_time: float = 1.0
@onready var countdown_label: Label = $Countdown

@onready var sprite_2d: Sprite2D = $Parent/Sprite2D
@onready var point_light_2d: PointLight2D = $Parent/PointLight2D

var attack: float = 0
var critical_rate: float = 0
var critical_bonus: float = 0
var knockback: float = 0
var affect_vertical_velocity: float = 0
var cam_shake_strength: float = 0
var cam_shake_duration: float = 0
var is_enemy: bool = false
var sender: Node2D = null

var is_shooted: bool = false


func _ready() -> void:
	sprite_2d.material = sprite_2d.material.duplicate()
	fade_show()


func _process(delta: float) -> void:
	if is_shooted: return
	
	countdown_time -= delta
	
	if countdown_time <= 0:
		is_shooted = true
		
		countdown_time = 0
		countdown_label.text = String.num(countdown_time, 1)
		
		gravity_attack()
		fade_hide()
		return
		
	countdown_label.text = String.num(countdown_time, 1)
	
	
func gravity_attack():
	var inst = (ResourceManager.get_resource("gravity_attack") as PackedScene).instantiate() as GravityAttack
	var level = get_tree().current_scene as Level
	level.particles_parent.add_child(inst)
	inst.sender = sender
	inst.attack = attack
	inst.critical_rate = critical_rate
	inst.critical_bonus = critical_bonus
	inst.knockback = knockback
	inst.affect_vertical_velocity = affect_vertical_velocity
	inst.cam_shake_duration = cam_shake_duration
	inst.cam_shake_strength = cam_shake_strength
	inst.set_is_enemy(is_enemy)
	
	inst.global_position = self.global_position + Vector2.DOWN * 150
	var target_pos = self.global_position + Vector2.UP * 150
	
	inst.start_attack(target_pos, false)
	
	
func fade_show():
	var tween_alpha = create_tween()
	tween_alpha.tween_property(sprite_2d.material, "shader_parameter/alpha", 1.0, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_OUT).from_current()
	
	tween_alpha = create_tween()
	tween_alpha.tween_property(point_light_2d, "energy", 2.0, 0.3) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_OUT).from_current()
	

func fade_hide():
	var tween_alpha = create_tween()
	tween_alpha.tween_property(sprite_2d.material, "shader_parameter/alpha", 0, 0.5) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_IN).from_current()
	
	tween_alpha = create_tween()
	tween_alpha.tween_property(point_light_2d, "energy", 0.0, 0.5) \
	.set_trans(Tween.TRANS_CUBIC) \
	.set_ease(Tween.EASE_OUT).from_current()
	
	await tween_alpha.finished
	queue_free()
	
